package com.dongdongshop.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.dk.data.R;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//地址管理
@Controller
@RequestMapping("address")
public class TbAddressController {

    @DubboReference
    TbAddressService ts;//地址信息

    @Autowired
    StringRedisTemplate stringRedisTemplate;



    //跳转页面
    @RequestMapping("tiao")
    public String tiao(){
        return "home-setting-address";
    }


    //查询全部
    @RequestMapping("getall")
    @ResponseBody
    public R<List<TbAddress>> getall() {
        String key="username";
        String username = stringRedisTemplate.boundValueOps(key).get();

        List<AddressVo> addVo = ts.getaddVo(username);//封装的vo类
        for(int i=0;i<addVo.size();i++){

            addVo.get(i).setMobile( DesensitizedUtil.mobilePhone(addVo.get(i).getMobile()) );//手机号脱敏
        }
        return R.ok().setData(addVo);
    }



    //三级联动第一级省
    @RequestMapping("getthree")
    @ResponseBody
    public R<List<TbProvinces>> getthree(){
        List<TbProvinces> ls = ts.getsanji();
        return R.ok().setData(ls);
    }


    //获取市
    @RequestMapping("getcity")
    @ResponseBody
    public R<List<TbCities>> getcity(String provinceid){
        List<TbCities> ls = ts.getcity(provinceid);
        return R.ok().setData(ls);
    }

    //获取县
    @RequestMapping("getarea")
    @ResponseBody
    public R<List<TbAreas>> getarea(String cityid){
        List<TbAreas> ls = ts.getarea(cityid);
        return R.ok().setData(ls);
    }

    //增加地址
    @RequestMapping("insertadd")
    @ResponseBody
    public R insertadd(TbAddress ta){
        ts.insertadd(ta);
        return R.ok();
    }

    //修改回显
    @RequestMapping("byname")
    @ResponseBody
    public R<TbAddress> upbyid(Long id){
        TbAddress ta = ts.upbyid(id);
        return R.ok().setData(ta);
    }


    //修改
    @RequestMapping("updateadd")
    @ResponseBody
    public R updateadd(TbAddress ta){
        ts.updateadd(ta);
        return R.ok();
    }


    //删除
    @RequestMapping("deletebyid")
    @ResponseBody
    public R deletebyid(Long id){
        ts.deletebyid(id);
        return R.ok();
    }






}
